package com.android.rapidcash.activitys

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.os.CountDownTimer
import android.text.SpannableStringBuilder
import androidx.core.view.isGone
import androidx.core.widget.addTextChangedListener
import com.android.lib.base.baseclass.BaseApplication
import com.android.lib.base.baseclass.BaseVMActivity
import com.android.lib.base.ext.*
import com.android.lib.base.manager.LiveBusManager
import com.android.lib.base.manager.MMKVManager
import com.android.rapidcash.dialog.TipDialog
import com.android.rapidcash.utils.CommUtils
import com.android.rapidcash.viewmodel.MainViewModel
import com.arc.fast.span.appendFastTextStyle
import com.arc.fast.span.enableClickableSpan
import com.blankj.utilcode.util.ToastUtils
import com.rapidcash.abanca.santander.vivus.revolut.waylet.libertex.icmarkets.caixabank.R
import com.rapidcash.abanca.santander.vivus.revolut.waylet.libertex.icmarkets.caixabank.databinding.ActivityLoginBinding
import java.util.Timer

class LoginActivity : BaseVMActivity<MainViewModel, ActivityLoginBinding>() {
    //1正常登录 2，进入进件页面 3,进入我的页面
    private val mLoginToPage by lazy {
        intent.getIntExtra("loginToPage", 1)
    }

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.ll2.chip(10)
        mBinding.ll5.chip(10)
        mBinding.ll3.chip(20)
        mBinding.tv5.chip(50)

    }

    override fun initData() {
        initString()

//        viewModel.loginForSms("18325017026", "9490")
//        viewModel.index()
//        viewModel.custInfoBasicQuery()
//        viewModel.getIdentificationResult()
//        viewModel.getAppValueList()
//        viewModel.vipProducts()
    }

    override fun initListener() {
        mBinding.ivBack.click { finish() }
        mBinding.et2.addTextChangedListener {
            if (mBinding.et2.text.toString().isNotEmpty()) {
                mBinding.tv5.setBackgroundResource(R.drawable.shape_login_3)
                mBinding.tv5.tag = "1"
                mBinding.iv3.visible()
            } else {
                mBinding.tv5.tag = "0"
                mBinding.tv5.setBackgroundColor(Color.parseColor("#E7E7ED"))
                mBinding.iv3.gone()
            }
        }

        mBinding.et1.addTextChangedListener {
            if (mBinding.et1.text.toString().isNotEmpty()) {
                mBinding.iv2.visible()
            } else {
                mBinding.iv2.gone()
            }

        }
        mBinding.iv2.click { mBinding.et1.setText("") }
        mBinding.iv3.click { mBinding.et2.setText("") }

        mBinding.tv6.click(2000) {
            if (mBinding.et1.text.isEmpty()) {
                ToastUtils.showShort(getString(R.string.login_10))
                return@click
            }
            viewModel.getVerifCode(mBinding.et1.text.toString())
        }

        mBinding.tv5.click(2000) {
            if (mBinding.tv5.tag == "0") return@click
            if (mBinding.tv4.text.isEmpty()) {
                ToastUtils.showShort(getString(R.string.login_10))
                return@click
            }
            viewModel.loginForSms(mBinding.et1.text.toString(), mBinding.et2.text.toString())
        }
    }


    override fun initObserve() {
        viewModel.getVerifCode.observe(this) {
            mBinding.et2.setText(it.singleSunRoundaboutArithmetic)
            mBinding.tv6.toggleVisibility()
            mBinding.tv4.toggleVisibility()
            startTime()
        }

        viewModel.loginForSms.observe(this) {
            log(it)
            MMKVManager.encodeString("token", it.irishPacificPotato)
            MMKVManager.encodeString("userID", it.russianGasStrictSteam)
            MMKVManager.encodeString("isNew", it.rawFox)
            MMKVManager.encodeString("phone", mBinding.et1.text.toString())
            LiveBusManager.postInt("Login", mLoginToPage)
            finish()
        }

    }

    private var timer: CountDownTimer? = null

    private fun startTime() {
        val lp = mBinding.tv4.layoutParams
        lp.width = 200.dp2px()
        mBinding.tv4.layoutParams = lp
        timer = object : CountDownTimer(60000, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                mBinding.tv4.text = getString(R.string.login_8) + " ${millisUntilFinished / 1000}"

            }

            override fun onFinish() {
                mBinding.tv4.text = getString(R.string.login_9)
                val lp2 = mBinding.tv4.layoutParams
                lp2.width = 130.dp2px()
                mBinding.tv4.layoutParams = lp2
            }

        }
        timer!!.start()
    }

    override fun onDestroy() {
        super.onDestroy()
        timer?.cancel()
        timer = null
    }


    private fun initString() {
        val spannableStringBuilder = SpannableStringBuilder()

        spannableStringBuilder.append(getString(R.string.login_1) + " ")

        spannableStringBuilder.appendFastTextStyle(
            "<${getString(R.string.login_2)}>"
        ) {
            textColor = Color.parseColor("#FE5A59")
            onClick = {
                startActivity(Intent(BaseApplication.getContext(), WebActivity::class.java).apply {
                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    putExtra("url", "https://www.baidu.com")
                    putExtra("title", getString(R.string.per_info2))
                })
            }
        }
        mBinding.tvInfo.text = spannableStringBuilder
        mBinding.tvInfo.enableClickableSpan() // 启用点击事件

    }

}